package dart

class GamesController {
	def index = {
		int numCompleted = Game.countByStatus(Game.COMPLETED)
		int numInProgress = Game.count() - numCompleted;

		if (numInProgress == 0) {
			redirect(action: list, params: [show: 'completed'])
			return
		}

		return [numInProgress: numInProgress,
				numCompleted: numCompleted]
	}

//--List Games----------------------------------------------------------------
	def list = {
		return params
	}

	def show = {
		Game game = Game.get(params.id)
		if (!game) {
			redirect(action: index)
			return
		}

		return [game: game]
	}

	def delete = {
		Game game = Game.get(params.id)

		try {
			game.delete(flush: true)
			flash.message = "Game deleted"
			redirect(action: index)
		} catch (Exception e) {
			flash.message = "Game could not be deleted"
			redirect(action: index)
		}
	}

}
